/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import java.io.File;
import java.util.ArrayList;

/**
 * A library of function for file access
 * @author chocoseb
 */
public abstract class ChocoFileAccess {

    /**
     * Create a directory if it does not already exist
     * @param p_directoryName The path and name of the directory
     * @return
     * 1 if it is a succes <br/>
     * -1 if the directory as not been created <br/>
     * -2 if the directory already existed <br/>
     * -3 if an Exception had occured
     */
    public static int createDirIfNotExist(String p_directoryName) {
        java.io.File f = new java.io.File(p_directoryName);
        try {
            if (!f.exists()) {
                if (f.mkdir()) {
                    return 1;

                } else {
                    return -1;
                }
            } else {
                return -2;
            }

        } catch (Exception e) {
            return -3;
        }
    }

    public static ArrayList<File> listSaveDirectories() {
        String s = "D:/prog/chocobudjet/ChocoBudjet/";
      //  String s = ChocoFileAccess.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        s = s.substring(0, s.lastIndexOf("/"));
        System.out.println(s);
        File f = new File(s);
        ArrayList<File> directories = new ArrayList<File>();
        for (File file : f.listFiles()) {
            if (file.isDirectory()) {
                for (File subFile : file.listFiles()) {
                    if (subFile.getName().equals("year.bdj")) {
                        directories.add(file);
                    }
                }
            }
        }
        return directories;
    }
}
